package wang.zl.bookmanager1.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import wang.zl.bookmanager1.R.ResultInfo;
import wang.zl.bookmanager1.domain.Book;
import wang.zl.bookmanager1.service.impl.BookServiceImpl;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 黑马程序员
 * @since 2022-12-06
 */
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    BookServiceImpl bookServiceImpl;

    @PostMapping("findAll")
    public ResultInfo findAll(){
        LambdaQueryWrapper<Book> qw = new LambdaQueryWrapper<>();
        qw.orderByAsc();
        List<Book>  list= bookServiceImpl.list(qw);
        if(list.size()!=0){
            return new ResultInfo(true,list,"成功");
        }else {
            return new ResultInfo(false,null,"暂无数据");
        }

    }

    @PostMapping("getbylike")
    public ResultInfo getbylike(String text,String type){
        if(text == null||type == null){
            text = "";
            type = "";
        }
        LambdaQueryWrapper<Book> qw = new LambdaQueryWrapper<>();
        if(type.equals("书籍名称")){
            qw.like(Book::getBookName,text).orderByAsc();
        }else{
            qw.like(Book::getAuthor,text).orderByAsc();
        }
        List<Book> list = bookServiceImpl.list(qw);
        if (list.size()!=0){
            return new ResultInfo(true,list,"成功");
        }else {
            return new ResultInfo(false,null,"暂无数据");
        }


    }
    @PostMapping("AddBook")
    public ResultInfo addBook(Book book){
        LambdaQueryWrapper<Book> qw = new LambdaQueryWrapper<>();
        qw.eq(Book::getBookName,book.getBookName());
        Book one = bookServiceImpl.getOne(qw);
        if(one == null){
            bookServiceImpl.save(book);
            return new ResultInfo(true,null,"添加成功");
        }
        return new ResultInfo(false,null,"已经存在了");
    }
    @PostMapping("UpdateBook")
    public ResultInfo updateBook(Book book , Long BookId){
        LambdaQueryWrapper<Book> qw1 = new LambdaQueryWrapper<>();
        if(!BookId.equals(book.getId())){
            LambdaQueryWrapper<Book> qw = new LambdaQueryWrapper<>();
            qw.eq(Book::getId,book.getId());
            Book one = bookServiceImpl.getOne(qw);
            if(one != null){
                return new ResultInfo(false,null,"编号已经存在了,请换一个");
            }else {

                qw1.eq(Book::getBookName,book.getBookName());
                Book one1 = bookServiceImpl.getOne(qw);
                if (one1 == null) {
                    bookServiceImpl.removeById(BookId);
                    bookServiceImpl.save(book);
                    return new ResultInfo(true, null, "修改成功");
                }else {
                    return new ResultInfo(false,null,"名称有相同的");
                }
            }
        }else {
            bookServiceImpl.removeById(BookId);
            qw1.eq(Book::getBookName,book.getBookName());
            Book one1 = bookServiceImpl.getOne(qw1);
            if (one1 == null) {
                bookServiceImpl.save(book);
                return new ResultInfo(true, null, "修改成功");
            }else {
                return new ResultInfo(false,null,"名称有相同的");
            }

        }
    }
}

